import React from "react";

import PageContent from '../../PageContent';

import { Link} from 'react-router-dom';

import {isNotNull,ajax,showResultMessage} from '../../common';

import ProductDefineDataTablePanel from './ProductDefineDataTablePanel';

import ProductDefineEditTabPanel from './ProductDefineEditTabPanel';

import ProductDefineFormPanel from './ProductDefineEditFormPanel';

import ProductVersionDataTablePanel from './ProductVersionDataTablePanel';

import ProductRoleDefineDataTablePanel from './ProductRoleDefineListPanel';

import ProductStafferListPanel from './ProductStafferListPanel';

/** 
 *产品列表
*/
export default class ProductDefineListPanel extends React.Component {

	actionHandler = (actionEvent,panelInstance) => {

		return {
						        
			refresh  :  () => {
				panelInstance.loadData();
			},
									        
			addNew  :  () => {
				panelInstance.addNew();
			},
									        
			doDelete  :  () => {
				panelInstance.doDelete(actionEvent.params);
			},																		
			doSave  :  () => {
				panelInstance.doSave();
			},
												
		};
	}

	render() {

        /**路由设置 */
        const pageContentMap = {
            '/': {
                text: '产品列表',
                icon: '',
                getComponent: (owner, props) => {
                    return (<ProductDefineDataTablePanel {...props} actionHandler={this.actionHandler} key="productDefineList" />);
                }
            },
            '/editProductDefine': {
                text: '编辑产品信息',
                icon: 'edit',
                getComponent: (owner, props) => {
                    return (<ProductDefineEditPanel {...props} key="editProductDefine" />);
                }
            },
        }

		return (
			<PageContent {...this.props} pageInstance={this} contentMap={pageContentMap}>
				
			</PageContent>
		);
	}
}

/**
 * 编辑产品定义信息
 */
class ProductDefineEditPanel extends React.Component {

	constructor(props) {
		super(props);
		this.state = {
			formTitle: '',
			pageTitle : '',
		}
	}

	componentDidMount(){
		this.productDefine = this.props.location.state;
		this.setState({pageTitle : '编辑[' + this.productDefine.name + ']' });
		this.loadProductDefineData(this.productDefine.id);
	}

	loadProductDefineData = (productDefineId) => {
		if(isNotNull(this.productDefineEditFormRef)){
			this.productDefineEditFormRef.loadProductDefineData(productDefineId);
		}
		else if(isNotNull(this.productVersionListRef)){
			this.productVersionListRef.loadProductDefineData(productDefineId);
		}
		else if(isNotNull(this.productRoleListRef)){
			this.productRoleListRef.loadProductDefineData(productDefineId);
		}
		else if(isNotNull(this.productStafferListRef)){
			this.productStafferListRef.loadProductDefineData(productDefineId);
		}
	}

	getTabItemComponent = (component,owner, props) => {

		const {productDefine} = this;

		switch(component){
			case '/ProductDefineBasicInfoPanel' :
			return (<ProductDefineBasicInfoFromPanel 
					productDefineId={productDefine.id}
					ref={inst => this.productDefineEditFormRef = inst} 
				/>);

			break;
			case '/ProductVersionListInfoPanel' :
			return (<ProducVersionListPanel 
				productDefineId={productDefine.id}
				ref={inst => this.productVersionListRef = inst} 
			/>);
			break;
			case '/ProductRoleDefineListInfoPanel' :
			return (<ProducRoleDefineListPanel 
				productDefineId={productDefine.id}
				ref={inst => this.productRoleListRef = inst} 
			/>);
			break;
			case '/ProductTeamInfoPanel' :
			return (<ProductStafferListPanel 
				productDefineId={productDefine.id}
				ref={inst => this.productStafferListRef = inst} 
			/>);
			break;
			
		}
		

	}

	onTabChange = () => {
		const {productDefine} = this;
		this.loadProductDefineData(productDefine.id);
	}

	render(){

		const {pageTitle} = this.state;

		return (
			<React.Fragment>
				<ProductDefineEditTabPanel
					pageHistory={this.props.history}
					title={pageTitle}
					extra={<Link to="/">返回</Link>}
					getTabItemComponent={this.getTabItemComponent}
					onTabChange={this.onTabChange}
				/>
			</React.Fragment>
		);
	}
}

/**
 * 产品基本信息表单
 */
class ProductDefineBasicInfoFromPanel extends React.Component {

	componentDidMount(){
		this.productDefineId = this.props.productDefineId;
		this.loadProductDefineData(this.productDefineId);
	}

	loadProductDefineData = (productDefineId) => {
		ajax('PRODUCT/REQ/ProductDefine/getProductDefineById',{id:productDefineId},(data) => {
			this.productDefineEditFormRef.setFormData(data);
		});
	}

	actionHandler = (actionEvent,panelInstance) => {

		return {									
			doSave : () => {
				panelInstance.doSave({},(result) => {
					if(result.success === true){
						result.message = "保存完成";
					}
					else{
						result.message = "保存失败";
					}
					showResultMessage(result);
				});
			},
												
		};
	}

	render(){

		return <ProductDefineFormPanel ref={inst => this.productDefineEditFormRef = inst} actionHandler={this.actionHandler} />
	}
}

/**
 * 产品版本列表
 */
class ProducVersionListPanel extends React.Component {

	componentDidMount(){
		this.productDefineId = this.props.productDefineId;
		this.loadProductDefineData(this.productDefineId);
	}

	loadProductDefineData = (productDefineId) => {
		this.productVersionListRef.loadData({productDefineId : productDefineId });
	}

	onBeforeLoadData = () => {
		return {productDefineId : this.productDefineId};
	}

	actionHandler = (actionEvent,panelInstance) => {

		const {productDefineId} = this;

		return {
						        
			refresh : () => {
				this.loadProductDefineData(productDefineId)
			},
									        
			addNew : () => {
				panelInstance.addNew();
			},			        
			doDelete : () => {
				panelInstance.doDelete(isNotNull(actionEvent.params) ? actionEvent.params.id : null);
			},							
			doEdit : () => {
				panelInstance.doEdit(actionEvent.params);
			},							
			doSave : () => {
				panelInstance.doSave({productDefineId : productDefineId});
			},
												
		};
	}
	

	render(){

		return <ProductVersionDataTablePanel ref={inst => this.productVersionListRef = inst} actionHandler={this.actionHandler} onBeforeLoadData={this.onBeforeLoadData} />
	}
	
}

/**
 * 产品版本列表
 */
class ProducRoleDefineListPanel extends React.Component {

	componentDidMount(){
		this.productDefineId = this.props.productDefineId;
		this.loadProductDefineData(this.productDefineId);
	}

	loadProductDefineData = (productDefineId) => {
		this.productRoleListRef.loadData({productDefineId : productDefineId });
	}

	onBeforeLoadData = () => {
		return {productDefineId : this.productDefineId};
	}

	actionHandler = (actionEvent,panelInstance) => {

		const {productDefineId} = this;

		return {
						        
			refresh : () => {
				this.loadProductDefineData(productDefineId)
			},
									        
			addNew : () => {
				panelInstance.addNew();
			},			        
			doDelete : () => {
				panelInstance.doDelete(isNotNull(actionEvent.params) ? actionEvent.params.id : null);
			},							
			doEdit : () => {
				panelInstance.doEdit(actionEvent.params);
			},							
			doSave : () => {
				panelInstance.doSave({productDefineId : productDefineId});
			},
												
		};
	}
	

	render(){

		return <ProductRoleDefineDataTablePanel ref={inst => this.productRoleListRef = inst} actionHandler={this.actionHandler} onBeforeLoadData={this.onBeforeLoadData} />
	}
	
}

